<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function ajax(url) {
            const p = new Promise((resolve, reject) => {
                let xhr = new XMLHttpRequest()
                xhr.open('get', url)
                xhr.onreadystatechange = () => {
                    if (xhr.readyState == 4) {
                        if (xhr.status >= 200 && xhr.status <= 300) {
                            resolve(JSON.parse(xhr.responseText))
                        } else {
                            reject('请求出错')
                        }
                    }
                }
                xhr.send() //发送hppt请求
            })
            return p
        }
        let url = '/data.json'
        ajax(url).then(res => console.log(res)).catch(reason => console.log(reason))
    </script>
</body>

</html>